package com.bricker.laboratorymanagement.business.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bricker.laboratorymanagement.business.dao.ExperimentMapper;
import com.bricker.laboratorymanagement.business.entity.Experiment;
import com.bricker.laboratorymanagement.business.model.ExpSelectModel;
import com.bricker.laboratorymanagement.business.service.ExperimentService;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service

public class ExperimentServiceImpl extends ServiceImpl<ExperimentMapper, Experiment> implements ExperimentService {
    @Autowired
    private ExperimentMapper experimentMapper;
    @Override
    public Page<Experiment> getExPerimentByPage(ExpSelectModel expSelectModel) {
        Page<Experiment> page=new Page<>(expSelectModel.getCurrent(),expSelectModel.getSize());
        return page.setRecords(experimentMapper.getExPerimentByPage(page,expSelectModel));
    }

    @Override
    public void saveOrUpdateExPeriment(Long uId,List<Experiment> experiments) {

        if(!CollectionUtils.isEmpty(experiments)){
            for (Experiment experiment : experiments) {
                Long id=experiment.getId();
                if(id==null){
                    experiment.setCreatedBy(uId);
                    experiment.setCreatedTime(LocalDateTime.now());
                    experiment.setDelFlag("0");
                    experiment.setExEvaluate(5);
                }


                experiment.setUpdatedBy(uId);
                experiment.setUpdatedTime(LocalDateTime.now());
            }
            super.saveOrUpdateBatch(experiments);
        }



    }

    @Override
    public void deleteExperimentByIds(Long uId,List<Long> ids) {
        List<Experiment> experiments =super.list(Wrappers.<Experiment>lambdaQuery().in(Experiment::getId,ids));
        List<Experiment> newExperiments=experiments.stream().map(e->{
            e.setDelFlag("1");
            return e;
        }).collect(Collectors.toList());
        super.updateBatchById(newExperiments);
    }
}
